package stack;

import java.util.Stack;

public class StackTest {

    public static void main(String[] args) {

        LinkedListStack<Integer> stack = new LinkedListStack<Integer>();

        for (int i = 0; i < 5; i++) {
            stack.push(i);
            System.out.println(stack);
        }

        stack.pop();
        System.out.println(stack);

        String s = "[[[]]]";

        System.out.println("valid: " + isValid(s));
    }

    public static boolean isValid(String s) {
        Stack<Character> stacks = new Stack<>();
        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            if (c == '(' || c == '{' || c == '[') {
                stacks.push(c);
            } else {
                if (stacks.isEmpty()) return false;
                char topChar = stacks.pop();
                if (c == ')' && topChar != '(') return false;
                if (c == ']' && topChar != '[') return false;
                if (c == '}' && topChar != '{') return false;
            }
        }
        return stacks.isEmpty();
    }

}
